<?php


namespace app\admin\service;


use app\admin\validate\AuthGroup;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\common\service\BaseAdminService;
use think\exception\ValidateException;

class AuthGroupService extends BaseAdminService
{

    protected static $sceneLabel = [
        'add' => '角色添加',
        'edit' => '角色编辑',
        'access' => '角色赋权'
    ];

    /**
     * 添加/编辑角色
     * @param $data
     * @param string $scene
     * @return array|string
     */
    public static function storeRole($data,$scene='add'){
        $data['state'] = isset( $data['state'])? $data['state']:0;
        try {
            validate(AuthGroup::class)->scene($scene)->check($data);
        } catch (ValidateException $e) {
            return self::error($e->getError(),null, ['token'=>request()->buildToken()]);
        }
        $model = new AuthGroupModel();

        //编辑 赋权
        if ($scene !='add') {
            $result = $model->exists(true)->update($data);
        } else {
            $result = $model->create($data);
        }
        return self::getSceneMessage($result,$scene);
    }

}
